package com.zx.article.writer.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zx.article.util.FmtRequest;
import com.zx.article.writer.model.WriterModel;
import com.zx.article.writer.service.IWriterService;
import com.zx.article.writer.service.impl.WriterServiceImpl;

@WebServlet("/WriterServlet")
public class WriterServlet extends HttpServlet {

	private static final long serialVersionUID = 2342441896614299026L;
	private FmtRequest fmtRequest = new FmtRequest();
	private IWriterService writerService = new WriterServiceImpl();

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Object result = "未实现方法";
		String action = request.getParameter("action");
		switch (action) {
		case "get":
			result = get(request);
			break;
		case "getAll":
			result = getAll(request);
			break;
		case "del":
			result = del(request);
			break;
		case "add":
			result = add(request);
			break;
		case "getModel":
			result = getModel(request);
			break;
		case "updata":
			result = updata(request);
			break;
		case "reset":
			result = reset(request);
			break;
		case "rePassword":
			result = rePassword(request);
			break;
		default:
			break;
		}
		fmtRequest.write(response.getWriter(), result);
	}

	private List<WriterModel> getAll(HttpServletRequest request) {
		return writerService.getAll();
	}

	private String rePassword(HttpServletRequest request) {
		WriterModel parseModel = fmtRequest.parseModel(request, WriterModel.class);
		return writerService.updata(parseModel);
	}

	private String reset(HttpServletRequest request) {
		WriterModel parseModel = fmtRequest.parseModel(request, WriterModel.class);
		return writerService.reset(parseModel);
	}

	private String updata(HttpServletRequest request) {
		WriterModel parseModel = fmtRequest.parseModel(request, WriterModel.class);
		return writerService.updata(parseModel);
	}

	private WriterModel getModel(HttpServletRequest request) {
		return writerService.getModel(request.getParameter("account"));
	}

	private String add(HttpServletRequest request) {
		if (!request.getParameter("password").equals(request.getParameter("realPassword"))) {
			return "passError";
		}
		WriterModel parseModel = fmtRequest.parseModel(request, WriterModel.class);
		return writerService.add(parseModel);
	}

	private String del(HttpServletRequest request) {
		return writerService.del(request.getParameter("account"));
	}

	private Map<String, Object> get(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<>();
//		1-count
		WriterModel parseModel = fmtRequest.parseModel(request, WriterModel.class);
		int count = writerService.getCount(parseModel);
		map.put("count", count);
//		2-Data
		parseModel.setPageIndex(Integer.parseInt(request.getParameter("pageIndex")));
		parseModel.setPageLimit(Integer.parseInt(request.getParameter("pageLimit")));
		parseModel.setPageOn(true);
		List<WriterModel> list = writerService.get(parseModel);
		map.put("list", list);
		return map;
	}

}
